package com.aiyige.page.detail.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.backup.TagBackup;
import com.aiyige.model.moment.entity.Moment;
import com.aiyige.model.moment.entity.VideoCourse;
import com.aiyige.setup.util.SetupUtil;
import com.aiyige.utils.ListUtil;
import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by xiachao on 2017/11/9.
 */

public class VideoCourseFragment extends Fragment {

    View mView;
    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.ll_class_type)
    LinearLayout llClassType;
    @BindView(R.id.tv_class_type_tag)
    TextView tvClassTypeTag;
    @BindView(R.id.tv_class_type)
    TextView tvClassType;
    @BindView(R.id.ll_interest)
    LinearLayout llInterest;
    @BindView(R.id.tv_interest_tag)
    TextView tvInterestTag;
    @BindView(R.id.tv_interest)
    TextView tvInterest;
    @BindView(R.id.ll_crowd)
    LinearLayout llCrowd;
    @BindView(R.id.tv_crowd_tag)
    TextView tvCrowdTag;
    @BindView(R.id.tv_crowd)
    TextView tvCrowd;
    @BindView(R.id.ll_learning_target)
    LinearLayout llLearningTarget;
    @BindView(R.id.tv_learning_target_tag)
    TextView tvLearningTargetTag;
    @BindView(R.id.tv_learning_target)
    TextView tvLearningTarget;
    @BindView(R.id.ll_teachers)
    LinearLayout llTeachers;
    @BindView(R.id.tv_teachers_tag)
    TextView tvTeachersTag;
    @BindView(R.id.tv_teachers)
    TextView tvTeachers;
    @BindView(R.id.tv_others)
    TextView tvOthers;
    @BindView(R.id.tv_detail_tag)
    TextView tvDetailTag;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    Unbinder unbinder;
    Moment moment;
    int marginTop;
    VideoCourse videoCourse = new VideoCourse();

    public VideoCourseFragment() {

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public int getMarginTop() {
        return marginTop;
    }

    public void setMarginTop(int marginTop) {
        this.marginTop = marginTop;
    }


    public static VideoCourseFragment newInstance(Moment moment, int marginTop) {
        VideoCourseFragment dialogVideoInfo = new VideoCourseFragment();
        dialogVideoInfo.setMoment(moment);
        dialogVideoInfo.setMarginTop(marginTop);
        return dialogVideoInfo;
    }

    public Moment getMoment() {
        return moment;
    }

    public void setMoment(Moment moment) {
        this.moment = moment;
        try {
            if (Moment.SUBJECT_VIDEO_COURSE.equals(moment.getSubject())) {
                videoCourse = JSON.parseObject(moment.getMoreBackup(), VideoCourse.class);
            }
        }catch (Exception e){

        }
        initData();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        this.mView = inflater.inflate(R.layout.video_course_layout, null);
        unbinder = ButterKnife.bind(this, mView);
        return mView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }

    private void initData() {
        if (moment == null || !moment.getSubject().contains(Subject.video_course)) {
            LinearLayout.LayoutParams ll = (LinearLayout.LayoutParams) mView.getLayoutParams();
            if (ll != null) {
                ll.height = 0;
                mView.setLayoutParams(ll);
            }
            return;
        }
//        tvTitle.setText(moment.getTitle());

        llClassType.setVisibility(TextUtils.isEmpty(videoCourse.getCourseType()) ? View.GONE : View.VISIBLE);
        tvClassType.setText(TextUtils.isEmpty(videoCourse.getCourseType()) ? "" : videoCourse.getCourseType());



        String interestStr = getInterestStr(moment.getTags());
        tvInterest.setText(interestStr);
        llInterest.setVisibility(TextUtils.isEmpty(interestStr) ? View.GONE : View.VISIBLE);

        tvLearningTarget.setText(videoCourse.getLearningGoals());
        llLearningTarget.setVisibility(TextUtils.isEmpty(videoCourse.getLearningGoals()) ? View.GONE : View.VISIBLE);

        tvCrowd.setText(videoCourse.getSuitablePopulation());
        llCrowd.setVisibility(TextUtils.isEmpty(videoCourse.getSuitablePopulation()) ? View.GONE : View.VISIBLE);

        tvTeachers.setText(videoCourse.getTeacher());
        llTeachers.setVisibility(TextUtils.isEmpty(videoCourse.getTeacher()) ? View.GONE : View.VISIBLE);

        String tvOthersStr = getOthersTextViewString(getTagValue(SetupUtil.GROUP_TYPE_OTHER));
        if (TextUtils.isEmpty(tvOthersStr)) {
            tvOthers.setVisibility(View.GONE);
        } else {
            tvOthers.setVisibility(View.VISIBLE);
        }
        tvOthers.setText(tvOthersStr);

        tvDetail.setText(moment.getSummary());
        tvDetailTag.setVisibility(TextUtils.isEmpty(moment.getSummary()) ? View.GONE : View.VISIBLE);
    }


    private List<String> getTagValue(String groupType) {
        List<String> valueList = new ArrayList<>();
        List<TagBackup> tagBackupList = moment.getTags();
        if (TextUtils.isEmpty(groupType) || tagBackupList == null
                || tagBackupList.isEmpty()) {
            return null;
        }
        for (TagBackup tag : tagBackupList) {
            if (tag != null && !TextUtils.isEmpty(tag.getGroupType())
                    && groupType.equals(tag.getGroupType())) {
                valueList.add(tag.getName());
            }
        }
        return valueList;
    }

    private String getTextViewString(List<String> list) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer(32);
        for (String string : list) {
            if (sb.length() == 0) {
                sb.append(string);
            } else {
                sb.append(" " + string);
            }
        }
        return sb.toString();
    }

    public String getInterestStr(List<TagBackup> tagBackupList) {
        if (ListUtil.isEmpty(tagBackupList)) return "";
        List<TagBackup> avocationList = new LinkedList<>();
        for (TagBackup tagBackup : tagBackupList) {
            if (SetupUtil.GROUP_TYPE_AVOCATION.equals(tagBackup.getGroupType())) {
                avocationList.add(tagBackup);
            }
        }
        if (ListUtil.isEmpty(avocationList)) return "";
        try {
            StringBuilder stringBuilder = new StringBuilder();
            Iterator<TagBackup> iterator = avocationList.iterator();
            TagBackup first = iterator.next();
            stringBuilder.append(first.getName());
            while (iterator.hasNext()) {
                TagBackup item = iterator.next();
                stringBuilder.append("、").append(item.getName());
            }
            return stringBuilder.toString();
        } catch (Exception e) {
            return "";
        }
    }

    private String getOthersTextViewString(List<String> list) {
        if (list == null || list.isEmpty()) {
            return "";
        }
        StringBuffer sb = new StringBuffer(32);
        for (String string : list) {
            if (sb.length() == 0) {
                sb.append("#" + string);
            } else {
                sb.append(" #" + string);
            }
        }
        return sb.toString();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
